package net.aaa.hotel.controller;

import com.alibaba.fastjson.JSON;
import net.aaa.hotel.dao.HTypeDao;
import net.aaa.hotel.dao.impl.HTypeDaoImpl;
import net.aaa.hotel.entity.HType;
import net.aaa.hotel.entity.PageInfo;
import net.aaa.hotel.util.LayuiTable;
import net.aaa.hotel.util.TransBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/type")
public class HTypeController extends HttpServlet {
    private HTypeDao dao = new HTypeDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=UTF-8");
        String method = req.getParameter("method");
        if (method != null){
            switch (method){
                case "add":
                    addType(req,resp);
                    break;
                case "del":
                    delType(req,resp);
                    break;
                case "upd":
                    updType(req,resp);
                    break;
                case "show":
                    showInfo(req,resp);
                    break;
                case "showTable":
                    showTable(req,resp);
                    break;
            }
        }else {
            List<Map> maps = dao.queryAll();
            maps.forEach(s->{
                s.forEach((x,y)->{
                    System.out.println(x+" "+y);
                });
            });
            Map m = new HashMap();
            m.put("code",0);
            m.put("msg","");
            m.put("count",maps.size());
            m.put("data",maps);
            String s = JSON.toJSONString(m);
            PrintWriter out = resp.getWriter();
            out.write(s);
            out.flush();
            out.close();
        }
    }

    private void showTable(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PageInfo pageInfo = new PageInfo();
        String cu = req.getParameter("curr");
        String nu = req.getParameter("nums");
        String condition = req.getParameter("condition");
//            System.out.println("cu 11111 cu"+cu);
//            System.out.println("nu 11111 nu"+nu);
//            System.out.println("condition 11111 condition"+condition);
            int curr = Integer.parseInt(cu);
            int nums = Integer.parseInt(nu);
            int count = dao.getCount(condition);
//            System.out.println("count++++"+count);
            List<Map> allByPage = dao.getAllByPage(curr, nums,condition);
        allByPage.forEach(s->{
            s.forEach((x,y)->{
                System.out.println(x+" "+y);
            });
        });
     /*   System.out.println("allBypage++++++"+allByPage);
        System.out.println(condition);*/
            LayuiTable layuiTable = new LayuiTable(count,allByPage);
            String s = JSON.toJSONString(layuiTable);
            PrintWriter out = resp.getWriter();
            out.print(s);
            out.flush();
            out.close();
    }

    private void showInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String gid = req.getParameter("id");
        Map room = dao.searchType(gid);
        String string = JSON.toJSONString(room);
        PrintWriter out = resp.getWriter();
        out.print(string);
        out.flush();
        out.close();
    }

    private void updType(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String gid = req.getParameter("gid");
        Map map = dao.searchType(gid);
        req.setAttribute("gust",map);
        req.getRequestDispatcher("HTypeController?method=add").forward(req,resp);
    }

    private void delType(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String gid = req.getParameter("gid");
        int i = dao.telType(gid);
        if (i>0){
            resp.sendRedirect("room/show.jsp");
        }
    }

    private void addType(HttpServletRequest req, HttpServletResponse resp) {
        Map<String, String[]> map = req.getParameterMap();
        HType ht = new HType();
        TransBean.populate(ht,map);
        System.out.println(ht);
        int i = dao.addType(ht);
    }


    
}
